<?php


declare(strict_types=1);

namespace app\seller\controller\admin;

use app\seller\model\Brand as BrandModel;
use sheep\controller\Backend;
use think\facade\Db;
use think\Request;
use think\Response;

//品牌申请
class Brand extends Backend
{

    protected $model = null;

    protected function initialize()
    {
        $this->model = new BrandModel;
    }


    /**
     * 查看
     *
     * @return Response
     */
    public function list(Request $request): Response
    {
        $params = $this->request->only([
            'type'
        ]);

        $list = $this->model->where('type',$params['type'])->select();

        foreach($list as &$val){

            $val['category_text'] = $this->model->getCategoryNamesAttr(null, $val);
        }


        return success('获取成功', $list);
    }


    public function detail(Request $request,$id){
        $params = $this->request->only([
            'type'
        ]);

        $list = $this->model->where('type',$params['type'])->where('id',$id)->find();

        $list['category_text'] = $this->model->getCategoryNamesAttr(null, $list);

        return success('获取成功', $list);

    }



    /**
     * 申请添加
     *
     * @return \think\Response
     */
    public function add(Request $request)
    {
        $params = $this->request->only(['type','logo', 'trademark_registration_number', 'qualification_certificate', 'category_ids','expiration_date','auth_type']);

        $this->svalidate($params, ".add");

        $data = Db::transaction(function () use ($params) {

            $this->model->save($params);

        });

        return success('保存成功', $data);
    }


    /**
     * 重新申请
     *
     * @param  $id
     * @return \think\Response
     */
    public function edit(Request $request, $id)
    {
        $params = $this->request->only(['type','logo', 'trademark_registration_number', 'qualification_certificate', 'category_ids','expiration_date','auth_type']);

        $params['audit_status'] = '0';
        $params['remarks'] = '';

//        $this->svalidate($params);

        $id = intval($id);

        Db::transaction(function () use ($id, $params) {

            $data = $this->model->where('id',$id)->where('type',$params['type'])->find();
            $data->save($params);

        });

        return success('更新成功');
    }

    
//    public function audit($id){
//        $id = intval($id);
//        $params = $this->request->only(['status', 'remarks']);
//        if(in_array($params['status'],[1,2])) {
//            Db::transaction(function () use ($id,$params) {
//                $apply = $this->model->where('id',$id)->find();
//                $apply->audit_status = $params['status'] ?? '0';
//                $apply->remarks = $params['remarks'] ?? '';
//                $apply->save();
//            });
//
//            return success('审核成功');
//        }
//        return success('审核成功',$id);
//    }




}
